package com.hxzy.servlet.UmsAdmin;

import com.hxzy.entity.UmsAdmin;
import com.hxzy.service.UmsAdminService;
import com.hxzy.service.impl.UmsAdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/UmsAdmin/Insert")
public class Insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/UmsAdmin/insert.jsp").forward(req,res);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        UmsAdmin admin=new UmsAdmin();

        String username=req.getParameter("username");
        admin.setUsername(username);

        String password=req.getParameter("password");
        admin.setPassword(password);

        /**
         * 头像
         */
        String icon=req.getParameter("icon");
        admin.setIcon(icon);

        /**
         * 邮箱
         */
        String email=req.getParameter("email");
        admin.setEmail(email);

        /**
         * 昵称
         */
        String nickName=req.getParameter("nickName");
        admin.setNickName(nickName);

        /**
         * 备注信息
         */
        String note=req.getParameter("note");
        admin.setNote(note);

        /**
         * 创建时间
         */
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date createTime=sdf.parse(req.getParameter("createTime"));
            admin.setCreateTime(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /**
         * 最后登录时间
         */
        try {
            Date loginTime=sdf.parse(req.getParameter("loginTime"));
            admin.setLoginTime(loginTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /**
         * 帐号启用状态：0->禁用；1->启用
         */
        Integer status=Integer.parseInt(req.getParameter("status"));
        admin.setStatus(status);

        /**
         * 供应商
         */
        Long supplyId=Long.getLong(req.getParameter("supplyId"));
        admin.setSupplyId(supplyId);

        UmsAdminService service=new UmsAdminServiceImpl();
        boolean insert = service.insert(admin);

//        PrintWriter out=res.getWriter();
//        out.println( insert? "新增成功": "新增失败");
//        //输出刷新
//        out.flush();
//        out.close();
        String path=req.getContextPath();
        req.setAttribute("url",path+"/UmsAdmin/search");
        //转发
        req.getRequestDispatcher("/WEB-INF/jump.jsp").forward(req,res);
    }
}
